package com.liuning.graduationdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuning.graduationdemo.entity.User;
import com.liuning.graduationdemo.service.IUserService;
import com.liuning.graduationdemo.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import com.liuning.graduationdemo.service.IMedicalRecordService;
import com.liuning.graduationdemo.entity.MedicalRecord;
import org.springframework.web.bind.annotation.RequestMapping;
import com.liuning.graduationdemo.common.Result;
import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 病历表 前端控制器
 * </p>
 *
 * @author pumpkin
 * @since 2022-06-05
 */
@RestController
@RequestMapping("/medicalRecord")
public class MedicalRecordController {

    @Resource
    private IMedicalRecordService medicalRecordService;

    @Resource
    private IUserService userService;

    /**
     * 保存或更新
     * @param medicalRecord
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody MedicalRecord medicalRecord) {
        if (medicalRecord.getId() == null) {
            //新增
            medicalRecord.setDoctorId(TokenUtils.getCurrentUser().getId());
            String patientName = medicalRecord.getPatientName();
            //获取患者id
            User patientUser = userService.getOne(new QueryWrapper<User>().eq("username", patientName));
            if (patientUser == null) {
                return Result.error("501","此用户名不存在");
            }else {
                medicalRecord.setPatientId(patientUser.getId());
            }
        }
        return Result.success(medicalRecordService.saveOrUpdate(medicalRecord));
    }

    /**
     * 在视频通话界面进行新增病历
     * @param medicalRecord
     * @return
     */
    @PostMapping("/videoSave/{toChatUser}")
    public Result videoSave(@PathVariable(name = "toChatUser") String toChatUser, @RequestBody MedicalRecord medicalRecord) {
        if (medicalRecord.getId() == null) {
            //新增
            medicalRecord.setDoctorId(TokenUtils.getCurrentUser().getId());
            //获取患者id
            User patientUser = userService.getOne(new QueryWrapper<User>().eq("username", toChatUser));
            if (patientUser == null) {
                return Result.error("501","此用户名不存在");
            }else {
                medicalRecord.setPatientId(patientUser.getId());
            }
        }
        return Result.success(medicalRecordService.saveOrUpdate(medicalRecord));
    }

    /**
     * 删除单条数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        medicalRecordService.removeById(id);
        return Result.success();
    }

    /**
     * 查询全部数据
     * @return
     */
    @GetMapping
    public Result findAll() {
        return Result.success(medicalRecordService.list());
    }

    /**
     * 根据id查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(medicalRecordService.getById(id));
    }

    /**
     * 分页查找所有数据
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           @RequestParam(defaultValue = "") String patientName,
                           @RequestParam(defaultValue = "") String doctorName) {
        QueryWrapper<MedicalRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        return Result.success(medicalRecordService.findPage(new Page<>(pageNum, pageSize),patientName,doctorName));
    }

    @GetMapping("/personMedicalRecord")
    public Result findPersonMedicalRecord(@RequestParam Integer pageNum,
                                          @RequestParam Integer pageSize) {
        return Result.success(medicalRecordService.findPersonMedicalRecord(new Page<>(pageNum, pageSize),TokenUtils.getCurrentUser().getId()));
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/del/batch")
    public Result batchDelete(@RequestBody List<Integer> ids) {
        medicalRecordService.removeBatchByIds(ids);
        return Result.success();
    }


}

